package demo01.aop;


import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by ztt on 2017/9/25.
 */
@Aspect
@Component
public class Aop04Args {

    private Map<String,Integer> trackCounts = new HashMap<String,Integer>();

    /**
     * 第一个*返回任意类型
     * @param trackNumber
     */
    @Pointcut("execution(* demo01.bean.Performance.trackPlay(String)) && args(trackNumber)")
    public void trackPlayed(String trackNumber){}

    @Before("trackPlayed(trackNumber)")
    public void countTrack(String trackNumber){
        System.out.println("i am record..." + trackNumber + " people");
        int currentCount = getPlayCount(trackNumber);
        trackCounts.put(trackNumber, ++currentCount);
        System.out.println(trackCounts);
    }

    public int getPlayCount(String trackNumber) {
        return trackCounts.get(trackNumber)!=null?trackCounts.get(trackNumber):0;
    }

}
